package com.dj.mingkong.admin.biz.repository.database.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.system.vo.request.UserQueryVo;
import com.dj.mingkong.admin.biz.util.WrapperUtil;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.system.UserEo;
import com.dj.mingkong.admin.biz.repository.database.system.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Objects;

/**
 * 用户repository
 *
 * @author zenglonghui
 * @date: 2022/5/18 10:32
 */
@Repository
public class UserRepository extends BaseDatabaseRepository<UserMapper, UserEo, QueryVo> {
    public Page<UserEo> listPageByVo(UserQueryVo queryVo) {
        final QueryWrapper<UserEo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Objects.nonNull(queryVo.getId()), UserEo::getId, queryVo.getId())
                .eq(StringUtils.isNotBlank(queryVo.getUsername()), UserEo::getUsername, queryVo.getUsername())
                .like(StringUtils.isNotBlank(queryVo.getNickname()), UserEo::getNickname, queryVo.getNickname())
                .eq(StringUtils.isNotBlank(queryVo.getEmail()), UserEo::getEmail, queryVo.getEmail())
                .in(StringUtils.isNotBlank(queryVo.getSex()), UserEo::getSex,
                        Arrays.stream(queryVo.getSex().split(CommonConstants.SPLIT_COMMA)).toList())
                .in(StringUtils.isNotBlank(queryVo.getStatus()), UserEo::getStatus,
                        Arrays.stream(queryVo.getStatus().split(CommonConstants.SPLIT_COMMA)).toList())
                .eq(StringUtils.isNotBlank(queryVo.getGenStatus()), UserEo::getGenStatus, queryVo.getGenStatus());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }

    public UserEo oneByUsername(String username) {
        return lambdaQuery().eq(UserEo::getUsername, username).one();
    }
}
